/*
 *
 * Author: Allan <qimingos_lsk@163.com>, (C) 2005-2007
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 */
#include "include/ShowLyric.h"
#include "include/ShowLyricApp.h"
#include "include/SelLyricDlg.h"
struct {
	GList* ListLyrics;
	SongInfo info;
	gint nAppIndex;
}g_SelLyricInfo = {0};

GtkWidget * SelLyricDlgInit(GladeXML* xml, GList* ListLyrics, SongInfo info, gint nAppIndex)
{
	XML_CONNECT_SIGNAL2(xml, on_SelLyricComboxLyrics_changed, xml);
	XML_CONNECT_SIGNAL2(xml, on_SelLyricBtnOK_clicked, xml);
	XML_CONNECT_SIGNAL2(xml, on_SelLyricBtnCancel_clicked, xml);

	GtkWidget * pSelLyricDlg = glade_xml_get_widget(xml, "SelLyricDlg");
	if (g_SelLyricInfo.ListLyrics)
	{
		_FreeList(g_SelLyricInfo.ListLyrics);
	}
	g_SelLyricInfo.ListLyrics = ListLyrics;
	g_SelLyricInfo.info = info;
	g_SelLyricInfo.nAppIndex = nAppIndex;
	gchar szTitle[MAX_PATH];
	GtkWidget * pSelLyricComboxLyrics = glade_xml_get_widget(xml, "SelLyricComboxLyrics");
	GList* pList = g_list_first(g_SelLyricInfo.ListLyrics);
	int index = 0;
	while (pList)
	{
		LyricsListItem* pItem = pList->data;
		if (strlen(pItem->szArtist) > 0)
		{
			snprintf(szTitle, MAX_PATH, "%d %s - %s", index, pItem->szArtist, pItem->szTitle);
		}
		else
		{
			snprintf(szTitle, MAX_PATH, "%d %s", index, pItem->szTitle);
		}
		gtk_combo_box_append_text(GTK_COMBO_BOX(pSelLyricComboxLyrics), szTitle);
		index++;
		pList = g_list_next(pList);
	}
	gtk_combo_box_set_active(GTK_COMBO_BOX(pSelLyricComboxLyrics), nAppIndex);
	return pSelLyricDlg;
}
void on_SelLyricComboxLyrics_changed(GtkWidget* widget, GladeXML* xml)
{
	GtkWidget * title = glade_xml_get_widget(xml, "SelLyricLableTitle");
	GtkWidget * artist = glade_xml_get_widget(xml, "SelLyricLableArtist");
	int index = gtk_combo_box_get_active(GTK_COMBO_BOX(widget));
	GList* pList = g_list_first(g_SelLyricInfo.ListLyrics);
	while (pList)
	{
		LyricsListItem* pItem = pList->data;
		if (pItem->index == index)
		{
			gtk_label_set_text(GTK_LABEL(title), pItem->szTitle);
			gtk_label_set_text(GTK_LABEL(artist), pItem->szArtist);
			break;
		}
		pList = g_list_next(pList);
	}
}

void on_SelLyricBtnCancel_clicked(GtkWidget* wideget, GladeXML* xml)
{
	GtkWidget* pSelLyricDlg = glade_xml_get_widget(xml, "SelLyricDlg");
	gtk_widget_hide(pSelLyricDlg);
}

/**
 * \brief 
 * \param widget
 * \param user_data
 */
void on_SelLyricBtnOK_clicked(GtkWidget* widget, GladeXML* xml)
{
	GtkWidget* pSelLyricDlg = glade_xml_get_widget(xml, "SelLyricDlg");
	gtk_widget_hide(GTK_WIDGET(pSelLyricDlg));

	LyricDebug("正在下载歌词......\n");
	// 下载歌词
	//	SetAppState(AS_DowningLyric); 
	GtkWidget * pSelLyricComboxLyrics = glade_xml_get_widget(xml,
			"SelLyricComboxLyrics");

	int index = gtk_combo_box_get_active(GTK_COMBO_BOX(pSelLyricComboxLyrics));
	GList* pList = g_list_first(g_SelLyricInfo.ListLyrics);
	gchar szID[MAX_PATH] = {0};
	while (pList)
	{
		LyricsListItem* pItem = pList->data;
		if (pItem->index == index)
		{
			strncpy(szID, pItem->szID, MAX_PATH);
			break;
		}
		pList = g_list_next(pList);
	}
	DownLoadLyric(szID, g_SelLyricInfo.info, g_SelLyricInfo.nAppIndex);
}
